#ifndef QT_HECP_H
#define QT_HECP_H
//#define QT
#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

#define IN		/*parameter in*/
#define OUT	/*parameter out*/

#define HECP_METHDO_UNKNOWN	0
#define HECP_METHOD_GET		1
#define HECP_METHOD_POST	2
#define HECP_METHOD_QUERY	3
#define HECP_METHOD_DELETE	4

#define HECP_CLI_TYPE_UNKNOWN	0
#define HECP_CLI_TYPE_UDP		1
#define HECP_CLI_TYPE_TCP		2

#define STATUS_CODE_100	100
#define STATUS_CODE_101	101
#define STATUS_CODE_200	200
#define STATUS_CODE_201	201
#define STATUS_CODE_202	202
#define STATUS_CODE_203	203
#define STATUS_CODE_204	204
#define STATUS_CODE_205	205
#define STATUS_CODE_206	206
#define STATUS_CODE_300	300
#define STATUS_CODE_301	301
#define STATUS_CODE_302	302
#define STATUS_CODE_303	303
#define STATUS_CODE_304	304
#define STATUS_CODE_305	305
#define STATUS_CODE_307	307
#define STATUS_CODE_400	400
#define STATUS_CODE_401	401
#define STATUS_CODE_402	402
#define STATUS_CODE_403	403
#define STATUS_CODE_404	404
#define STATUS_CODE_405	405
#define STATUS_CODE_406	406
#define STATUS_CODE_407	407
#define STATUS_CODE_408	408
#define STATUS_CODE_409	409
#define STATUS_CODE_410	410
#define STATUS_CODE_411	411
#define STATUS_CODE_412	412
#define STATUS_CODE_413	413
#define STATUS_CODE_414	414
#define STATUS_CODE_415	415
#define STATUS_CODE_416	416
#define STATUS_CODE_417	417
#define STATUS_CODE_500	500
#define STATUS_CODE_501	501
#define STATUS_CODE_502	502
#define STATUS_CODE_503	503
#define STATUS_CODE_504	504
#define STATUS_CODE_505	505


#if defined(__unix__)

        #include <unistd.h>
        #include <sys/time.h>
        #include <netinet/in.h>
        #include <netinet/tcp.h>
        #include <sys/un.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netdb.h>
        #include <arpa/inet.h>


        #define net_close close
        typedef int hxSDK_socket_t;
        typedef pthread_t hxSDK_thread_t;
        typedef pthread_mutex_t hxSDK_mutex_t;
        typedef pthread_cond_t hxSDK_cond_t;

        #define hxSDK_thread_create		pthread_create
        #define hxSDK_thread_join		pthread_join
        #define hxSDK_thread_exit		pthread_exit
        #define hxSDK_mutex_init		pthread_mutex_init
        #define hxSDK_mutex_lock		pthread_mutex_lock
        #define hxSDK_mutex_unlock		pthread_mutex_unlock
        #define hxSDK_mutex_destroy		pthread_mutex_destroy


#elif defined(_WIN32)
        #include <windows.h>
        #include <winsock.h>
        #define net_close closesocket
        typedef SOCKET hxSDK_socket_t;
        typedef HANDLE hxSDK_thread_t;
        typedef HANDLE hxSDK_mutex_t;
        typedef HANDLE hxSDK_cond_t;
#endif
////add by Gaoy----20180112---start

#define HX_LINE_LOG_RESPONSE_MAGIK	0x20090218
typedef enum hx_mode_type
{
    HX_SDESC_MODE_UNKNOWN=0,
    HX_SDESC_MODE_AUTO=0,//use in hx_stream_p2p()/hx_file_playback();
    HX_SDESC_MODE_PASSIVE,
    HX_SDESC_MODE_PORT
}hx_mode_type;

typedef enum hx_proto_type
{
    HX_SDESC_PROTO_UNKNOWN=0,
    HX_SDESC_PROTO_AUTO=0,//use in hx_stream_p2p()/hx_file_playback();
    HX_SDESC_PROTO_TCP,
    HX_SDESC_PROTO_UDP,
    HX_SDESC_PROTO_RTP,
    HX_SDESC_PROTO_SAP,
    HX_SDESC_PROTO_UDGR,
    HX_SDESC_PROTO_FILE,
    HX_SDESC_PROTO_DEC,
    HX_SDESC_PROTO_DTCP,
}hx_proto_type;
typedef struct hx_datetime{
  int year;/*2007,2008,2009*/
  int mon;/*1-12*/
  int day;/*1-31*/
  int hour;/*0-23*/
  int min;/*0-59*/
  int sec;/*0-59*/
}hx_datetime_t;
typedef struct __linE_log_response
{
    unsigned long magiK;//must 0x20090218,BIG-ENDIAN
    hx_datetime_t tick;//all BIG-ENDIAN,{Year,Month,Day,Hour,Minute,Second}
    unsigned long level;//BIG-ENDIAN
    unsigned long module;//BIG-ENDIAN
    unsigned char status;
    char msg[256];
}linE_lres_t;
typedef struct _log_data_struct
{
    linE_lres_t msg;
    struct _log_data_struct*next;
}log_data_struct;

typedef struct _log_query_struct
{
    //struct hx_target_object*object;
    //int sock_fd;
    hxSDK_socket_t sock_fd;//edit by hanln
    log_data_struct*data;
    int data_len;
    //char ip[16];
    char ip[32];//modify by lishuai 2017.08.09
    int port;
    hx_proto_type proto;
    hx_mode_type mode;
//    int flush_signal[2];
    //pthread_t pid;
//    hxSDK_thread_t pid;//edit by hanln 2009-05-12
    struct _log_query_struct *next;
}log_query_struct;

///获取日志，将其返回在log_query_struct->data中
///仅仅需要传入IP 和 端口
log_query_struct* Getlogs(const char *ip,const int port);
////add by Gaoy----20180112---end

int QtHecp_mutex_init(hxSDK_mutex_t *mutex);///add by Gaoy---20180112
int QtHecp_mutex_lock(hxSDK_mutex_t *mutex);
int QtHecp_mutex_unlock(hxSDK_mutex_t *mutex);
int QtHecp_socket_close(hxSDK_socket_t sockfd);///add by Gaoy---20180112
///-----------------------------------------------------------------------

typedef struct hecp_entity_struct {
    char *key;
    int key_len;
    char *value;
    int value_len;
    struct hecp_entity_struct *next;
}hecp_entity_t;

typedef struct hecp_request_struct {
    int sockfd;/*TCP: new accepted sockfd*/
    struct sockaddr_in cli_addr;/*TCP/UDP fill it*/

    int method;/*HECP_METHOD_XXXX*/

    char rbuf[1024*24];/*request read buffer*/
    int rlen;/*request length*/

    char sbuf[1024*24];/*send buffer*/
    int slen;/*sending length*/

    char entity[1024*23];/*response entity*/
    int elen;/*entity length*/

    int status;/*response status code*/

    char *URL;/*uniform resource locator*/

    int nrt;/*number of entities in this request*/
    struct hecp_entity_struct *ety_p;/*entity list head*/
    struct hecp_entity_struct *ety_cp;/*current entity*/
}hecp_request_t;
///-----------------------------------------------------------------------
int hecp_cli_open_TCP(IN const char *dst_addr,
                      IN int dst_port);
int hecp_cli_set_request_head_URL(IN int cid,
        IN const char *URL);
int hecp_cli_set_request_head_method(IN int cid,
    IN int method);
int hecp_cli_add_request_entity(IN int cid,
        IN const char *key,
        IN const char *value);
int hecp_cli_transmit(IN int cid, struct timeval *timeout);
void hecp_cli_close(IN int cid);
int hecp_cli_get_response_head_status_code(IN int cid);
int hecp_cli_get_response_entity_next(IN int cid,
    OUT char *key,
    IN OUT int *key_len,
    OUT char *value,
    IN OUT int *value_len);
int hecp_cli_get_response_entity_first(IN int cid);
extern log_query_struct *log_head;

#ifdef __cplusplus
};
#endif

#endif // HECP_H

